package one.stand.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.spec.AlgorithmParameterSpec;

/**
 * Created by chengjiarui on 2019-06-26.
 */
public class DesUtil {
    private AlgorithmParameterSpec iv = null;// 加密算法的参数接口，IvParameterSpec是它的一个实现
    private SecretKey key = null;
    private String charset = "UTF-8";
    private String model = "DES/CBC/PKCS5Padding";

    public DesUtil(String desKey, String desIv) throws Exception {
        byte[] DESkey = desKey.getBytes();
        byte[] DESIV = desIv.getBytes();
        DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
        iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        key = keyFactory.generateSecret(keySpec);// 得到密钥对象
    }

    /**
     * 加密
     *
     * @param data 待加密的数据
     * @return 加密后的数据
     * @throws Exception
     */
    public String encode(String data) throws Exception {
        return encode(data.getBytes(charset));
    }

    public String encode(byte[] data) throws Exception {
        Cipher enCipher = Cipher.getInstance(model);// 得到加密对象Cipher
        enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式，给出密钥和向量
        byte[] pasByte = enCipher.doFinal(data);
        return new String(pasByte);
    }

    public byte[] encodeByte(byte[] data) throws Exception {
        Cipher enCipher = Cipher.getInstance(model);// 得到加密对象Cipher
        enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式，给出密钥和向量
        byte[] pasByte = enCipher.doFinal(data);
        return pasByte;
    }

    /**
     * 解密
     *
     * @param data 解密前的数据
     * @return 解密后的数据
     * @throws Exception
     */
    public String decode(String data) throws Exception {
        return decode(data.getBytes(charset));
    }

    public String decode(byte[] data) throws Exception {
        Cipher deCipher = Cipher.getInstance(model);
        deCipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] pasByte = deCipher.doFinal(data);
        return new String(pasByte, charset);
    }

    public byte[] decodeByte(byte[] data) throws Exception {
        Cipher deCipher = Cipher.getInstance(model);
        deCipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] pasByte = deCipher.doFinal(data);
        return pasByte;
    }

    public String getCharset() {
        return charset;
    }

//    public static void main(String[] args) throws Exception {
//        DesUtil desUtil = new DesUtil("lXySzByz", "lXySzByz");
//        byte[] pasByte = desUtil.encodeByte(fileToBytes("/Users/yandou/java/mmm/model.zip"));
//
//        bytesToFile(pasByte, "/Users/yandou/java/mmm/model_jm.zip");


////        DesUtil desUtil = new DesUtil("lXySzByz", "lXySzByz");
//        DesUtil desUtil = new DesUtil("12345678", "12345678");
////        byte[] pasByte = desUtil.decodeByte(fileToBytes("/Users/yandou/Downloads/1561637511898.zip"));
//        byte[] pasByte = desUtil.decodeByte(fileToBytes("/Users/yandou/Downloads/model1.3d"));
//        byte[] pasByte = desUtil.decodeByte(FileUtil.fileToBytes("/Users/yandou/java/mmm/o/model1.3d"));
//
//        FileUtil.bytesToFile(pasByte, "/Users/yandou/java/mmm/o/model1_3d.zip");
//    }
}
